package com.sy.demo.controller;

import com.sy.demo.model.Resume;
import com.sy.demo.service.ResumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author songyi
 * @date 2020/6/17 0017 20:47
 * @description
 */
@Controller
@RequestMapping("/resume")
public class ResumeContriller {

    @Autowired
    private ResumeService resumeService;

    // 查询所有
    @RequestMapping("/all")
    @ResponseBody
    public Map<String, Object> findAll() {
        Map<String, Object> map = new HashMap<>();
        List<Resume> all = resumeService.findAll();
        if (all.size() > 0) {
            map.put("code", 0);
            map.put("msg", "成功");
            map.put("count", all.size());
            map.put("data", all);
        } else {
            map.put("code", 1);
            map.put("msg", "失败");
            map.put("count", 0);
            map.put("data", null);
        }
        return map;
    }

    // 新增
    @RequestMapping("/save")
    @ResponseBody
    public Map<String, Object> inser(Resume resume) {
        Map<String, Object> map = new HashMap<>();
        Resume save = resumeService.save(resume);
        if (save != null) {
            map.put("code", 0);
            map.put("msg", "成功");
            return map;
        } else {
            map.put("code", 1);
            map.put("msg", "失败");
            return map;
        }
    }

    // 修改
    @RequestMapping("/update")
    @ResponseBody
    public Map<String, Object> update(Resume resume) {
        Map<String, Object> map = new HashMap<>();
        Resume save = resumeService.save(resume);
        if (save != null) {
            map.put("code", 0);
            map.put("msg", "成功");
            return map;
        } else {
            map.put("code", 1);
            map.put("msg", "失败");
            return map;
        }
    }

    // 删除
    @RequestMapping("/del")
    @ResponseBody
    public Map<String, Object> del(Resume resume) {
        Map<String, Object> map = new HashMap<>();
        // 删除操作
        resumeService.del(resume.getId());
        try {
            // 查询是否存在
            Resume byId = resumeService.findById(resume.getId());
        } catch (Exception e) {
            map.put("code", 0);
            map.put("msg", "成功");
            return map;
        }
        map.put("code", 1);
        map.put("msg", "失败");
        return map;
    }
}
